﻿#pragma once
#include "pch.h"
#include <Network/TcpClient.h>
#include <Poller/Timer.h>

class TcpClient
    : public toolkit::TcpClient
{
public:
    void Init(const std::string& ip, int port, float out_time = 3.0);
    void SetAutoReconnect(bool flag = true, float reconnect_time = 5.0);
    bool Connect();
    bool IsAlive();
    void Disconnect();
    void SendData(const char* data, int len);
    void SetRcvDataCallback(std::function<void(const toolkit::Buffer::Ptr&)>);
    void SetConnectedCallback(std::function<void()>);

protected:
    // 通过 TcpClient 继承
    void onRecv(const toolkit::Buffer::Ptr& buf) override;
    void onError(const toolkit::SockException& err) override;
    void onConnect(const toolkit::SockException& ex) override;

private:
    std::string _ip;
    int _port = -1;
    bool _auto_reconnect = true;
    float _reconnect_time = 5.0f;
    
    float _out_time = 3.0f;
    toolkit::Timer::Ptr _timer = nullptr;
    std::function<void(const toolkit::Buffer::Ptr&)> _data_callback = nullptr;
    std::function<void()> _connected_callback = nullptr;
};
